package sort.simple;

import org.junit.Test;


public class SelectionSort {
    @Test
    public void test() {
        int[] a = {5, 2, 7, 4, 1, 3, 8, 9};
        selectionSort(a);
        for (int i = 0; i < a.length; i++) {
            System.out.print(a[i]+" ");
        }
    }

    /**
     * 选择排序
     * @param nums
     */
    public void selectionSort(int[] nums) {
        for (int i = 0; i < nums.length - 1; i++) {
            int minIndex = i;       //minIndex要找到本轮无序序列的最小值
            for (int j = i + 1; j <nums.length; j++) {
                if (nums[minIndex] > nums[j]) {
                    minIndex = j;
                }
            }
            swap(i, minIndex, nums);
        }
    }

    public void swap(int i, int j, int[] nums) {
        int tmp = nums[i];
        nums[i] = nums[j];
        nums[j] = tmp;
    }
}